<?php
    include_once("../action/checkAuthenticationAction.php");
    include_once("../service/productService.php");
	include_once("../service/receiptService.php");
	include_once("../service/userService.php");
    include_once("../util/constant.php");
    include_once("../util/message.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Chi tiết Hóa đơn</title>
	<?php 
        include("import-css.php");
    ?>
    <style>
    table td { vertical-align: middle !important; }
    </style>
</head>
<body>
    <?php include("header.php");?>
    
	<div class="container-fluid">
		<div class="row-fluid">
            <?php include_once("menu.php");?>
			<div id="content" class="span10">
            	<?php
                    $class = null;
                    $message = null;
                    
                    if(isset($_GET["action"])){
                        $action = $_GET["action"];
                        
                        if($action == UPDATE_FAILED){
                            $message = $messages["editReceiptFailed"];
                            $class = FAILED_MESSAGE_CLASS;
                        } else if($action == UPDATE_FAILED."1"){
                            $message = $messages["editActuallyDeliveryingDateOfReceiptFailed"];
                            $class = SUCCESSFULL_MESSAGE_CLASS;
                        }else if($action == UPDATE_SUCCESSFULLY){
                            $message = $messages["editReceiptSuccessfully"];
                            $class = SUCCESSFULL_MESSAGE_CLASS;
                        }
				?>
                		<div id="msg" class="center alert <?php showClass($class);?>"><?php showMessage($message);?></div>		
                <?php
                    }
                ?>
                <div class="box">
                	<div class="box-content">
						<?php
							if(!authUtil(array("edit receipt","view receipt"),$perm_array,false)){
								echo "<script>window.location.href='../admin/index.php';</script>";
							}
						?>                    		
                    	 <form id="form" action="" method="post" class="form-horizontal">
                         	<?php
								$recId = $_GET["id"];
								$receipt = mysql_fetch_array(findReceiptBy($recId));
                            ?>
                         	<div class="control-group">
                                <label for="categoryTitle" class="control-label">Khách hàng</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input">
                                    	<?php
											$customer = mysql_fetch_array(findUserById($receipt["USER_ID"]));
											echo $customer["USER_EMAIL"]." - ".$customer["USER_USERNAME"];
                                        ?>
                                    </span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label for="categoryTitle" class="control-label">Mã</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input"><?php echo $receipt["REC_CODE"];?></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label for="categoryTitle" class="control-label">Ngày tạo</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input">
                                    	<?php 
											$date = new DateTime($receipt["REC_DATE"]);
											echo $date->format('d/m/Y');
										?>
                                    </span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label for="status" class="control-label">Trạng thái</label>
                                <div class="controls">
                                    <select name="status" data-rel="chosen" id="status">
                                    	<?php
											$statuses = findAllStatus();
											
											while($status = current($statuses)){
												if(key($statuses) == $receipt["REC_STATUS"]){
                                        ?>
													<option value="<?php echo key($statuses); ?>" selected><?php echo($status);?></option>
                                        <?php
												} else{	
										?>            
                                        			<option value="<?php echo key($statuses); ?>"><?php echo($status);?></option>
                                        <?php
												}
												next($statuses);
											 }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">Địa chỉ giao hàng</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input"><?php echo $receipt["REC_DELIVERYINGADDRESS"];?></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">Địa chỉ trả tiền</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input"><?php echo $receipt["REC_PAYINGADDRESS"];?></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">Ngày giao hàng</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input">
                                    	<?php 
											$deliveryingDate = new DateTime($receipt["REC_DELIVERYINGDATE"]);
											echo $deliveryingDate->format('d/m/Y');
										?>
                                    </span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">Ngày giao hàng thật sự</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input">
                                    	<?php 
											if($receipt["REC_ACTUALLYDELIVERYINGDATE"] != ""){
												$actuallyDeliveryingDate = new DateTime($receipt["REC_ACTUALLYDELIVERYINGDATE"]);
												echo $actuallyDeliveryingDate->format('d/m/Y');
											}
										?>
                                    </span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">Số điện thoại</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input"><?php echo $receipt["REC_PHONENUMBER"];?></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">Ghi chú</label>
                                <div class="controls">
                                    <textarea rows="7" style="width: 400px;" name="note"><?php echo $receipt["REC_NOTE"];?></textarea>
                                </div>
                            </div>
							<table class="table table-striped table-bordered bootstrap-datatable">
								<thead>
									<tr>
                                        <th>STT</th>
                                        <th>Hình ảnh</th>
                                        <th>Tên</th>
                                        <th>Số lượng</th>
                                        <th>Giá</th>
                                        <th>Lựa chọn</th>
                                        <th>Ghi chú</th>                                     
                                    </tr>
								</thead>
                                <tbody>
									<?php
										$receiptItems = findReceiptItemBy($recId);
										$count = 1;
										while($row = mysql_fetch_array($receiptItems)){
				                    ?>    	
                                            <tr>
                                                <td><?php echo $count++; ?></td>
                                                <td>
                                                    <?php
                                                        $image = mysql_fetch_array(findLimitedImagesOfProduct($row["PRO_ID"], 1));
                                                        echo "<img src='../image/product/".$row["PRO_ID"]."/thumbnail/".$image["IMG_PATH"]."' width='60' height='60'/>";
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                        $product = findProductBy($row["PRO_ID"]); 
                                                        echo $product["PRO_NAME"]." (".$product["PRO_CODE"].")";
                                                    ?>
                                                </td>
                                                <td><?php echo $row["REI_QUANTITY"];?></td>
                                                <td><?php echo number_format($row["REI_PRICE"]);?></td>
                                                <td>
                                                    <?php
                                                        $optionItems = findReceiptProductOptionItemBy($row["PRO_ID"], $row["REC_ID"]);
                                                        
                                                        while($optionItem = mysql_fetch_array($optionItems)){
                                                            echo "- ".$optionItem["OPTIONITEM"]."<br/>";
                                                        }
                                                    ?>
                                                </td>
                                                <td><?php echo $row["REI_NOTE"]; ?></td>
                                            </tr>
									<?php
                                        }
                                    ?>	
                                </tbody>
                            </table>
                            <div class="form-actions">
                            	<?php
									$require_perm = array("edit receipt");
									if(authUtil($require_perm,$perm_array,true)){
								?>
                            		<button type="button" class="btn btn-primary" onclick="submitForm('form', '../action/editReceiptAction.php', 'post');">Chỉnh sửa</button> 
                                <?php
									}
                                ?>
                                <button type="button" class="btn btn-primary" onclick="$('#form').attr('target', '_blank');submitForm('form', './print-receipt.php?id=<?php echo $recId;?>', 'post');">In hóa đơn</button>
                            </div>
                            <input type="hidden" name="recId" value="<?php echo $recId;?>"/>
                         </form>
					</div><!--end of box content-->
				</div><!--end of box-->
			</div><!--end of box content-->
		</div><!--end of box row fluid-->
        <?php include("footer.php");?>
        <?php include("import-js.php");?>
	    <?php include("import-js-upload.php");?>
	</div><!--end of box container fluid-->
</body>
</html>